<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/list.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <style>
        
        .nav{
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 50px;
        }
        .container{
            padding-top: 80px;
            height: auto;
        }
        .container-right{
            width: auto;
        }
        .blog-pagnation-wrapper{
            height: 40px;
            margin: 16px 0;
            text-align: center;
        }
        .blog-pagnation-item{
            display: inline-block;
            padding: 8px;
            border: 1px solid #d0d0d5;
            color: #333;
        }
        .blog-pagnation-item:hover{
            background: #4e4eeb;
            color: #fff;
        }
        .blog-pagnation-item.actvie{
            background: #4e4eeb;
            color: #fff;
        }

    </style>
    <script src="js/jquery.min.js"></script>
    <script src="js/Tool.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="login.html">登陆</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 右侧内容详情 -->
        <div class="container-right" style="width: 100%;">
            <!-- 每一篇博客包含标题, 摘要, 时间 -->
            <div id="listDiv">

            </div>
            <hr>
            <div class="blog-pagnation-wrapper">
                <button class="blog-pagnation-item" onclick="firstClick()">首页</button> 
                <button class="blog-pagnation-item" onclick="beforeClick()">上一页</button>  
                <button class="blog-pagnation-item" onclick="nextClick()">下一页</button>
                <button class="blog-pagnation-item" onclick="lastClick()">末页</button>
            </div>
        </div>
    </div>
</body>
<script>
     var descLen = 200;//简介最大长度
    //字符串截取，将文章正文截取成简介
    function mySubstr(content) {
        if(content.length>descLen) {
            return content.substr(0,descLen);
        }
        return content;
    }
    var pindex = 1;//当前的页码
    var psize = 2;//每页显示的条数信息
    var totalpage = 1;//总页数

    //初始化分页参数，尝试从url中获取pindex和psize
    function initPageParam() {
        var pi = getURLParam("pindex");
        if(pi!="") {
            pindex=pi;
        }
        var ps = getURLParam("psize");
        if(ps!="") {
            psize=ps;
        }
    }
    initPageParam();

    //查询分页数据
    function getList() {
        jQuery.ajax({
            url:"/art/list",
            type:"GET",
            data:{
                "pindex":pindex,
                "psize":psize
            },
            success:function(result) {
                if(result.code==200 && result.data!=null && result.data.length>0) {
                    //循环拼接控制document
                    var finalHtml = "";
                    for(var i=0;i<result.data.length;i++) {
                        var item = result.data[i];
                        //如果state==2说明是草稿箱的文章，不显示出来
                        if(item.state==2) {
                            continue;
                        }
                        finalHtml+='<div class="blog">';
                        finalHtml+='<div class="title">'+item.title+'</div>';
                        finalHtml+='<div class="date">'+item.createtime+'</div>';
                        finalHtml+='<div class="desc">'+mySubstr(item.content)+'</div>';
                        finalHtml+='<a href="blog_content.html?id='+item.id+'" class="detail">查看全文</a>';
                        finalHtml+='</div>';
                    }
                    jQuery("#listDiv").html(finalHtml);
                }
            }
        });
    }
    getList();

    //查询总共多少页
    function getTotalPage() {
        jQuery.ajax({
            url:"/art/totalpage",
            type:"GET",
            data:{
                "psize":psize
            },
            success:function(result) {
                if(result.code==200 && result.data!=null) {
                    totalpage=result.data;
                }
            }
        });
    }
    getTotalPage();

    //首页
    function firstClick() {
        location.href="blog_list.html";
    }

    //上一页
    function beforeClick() {
        if(pindex<=1) {
            alert("前面已经没有内容了！");
            return false;
        }
        pindex-=1;
        location.href="blog_list.html?pindex="+pindex+"&psize="+psize;
    }

    //下一页
    function nextClick() {
        pindex = parseInt(pindex);
        if(pindex>=totalpage) {
            //已经是最后一页
            alert("后面已经没有内容了哦！");
            return false;
        }
        pindex+=1;
        location.href="blog_list.html?pindex="+pindex+"&psize="+psize;
    }

    //末页
    function lastClick() {
        pindex=totalpage;
        location.href="blog_list.html?pindex="+pindex+"&psize="+psize;
    }
</script>
</html>